package db;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import db.adapter.DbAdapter;


public class DbTable implements DbTableInterface {

	protected Connection con;
	private Statement st = null;
	private ResultSet rs = null;

	protected String _getName() {
		return null;
	}

	protected void _setAdapter() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
		con = null;
	}

	public DbTable() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
		_setAdapter();

	}

	public DbTable(DbAdapter adapter) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
		con = adapter.getConnection();
	}

	@Override
	public DbSelectInterface select() {
		return new DbSelect(_getName());
	}

	@Override
	public Object fetchAll(DbSelectInterface select) throws SQLException {

		ArrayList<DbRow> ArrResult = null;
		try {
			st = con.createStatement();
			System.out.println(select.toString());
			rs = st.executeQuery(select.toString());
			
			//System.out.println(select.toString());
			ResultSetMetaData rsmd;
			rsmd = rs.getMetaData();

			int n = rsmd.getColumnCount();

			ArrResult = new ArrayList<DbRow>();

			while (rs.next()) {
				DbRow row = new DbRow(n);
				for (int i = 0; i < n; i++) {
					row.setRow(rs.getObject(i + 1), i,
							rsmd.getColumnName(i + 1));

				}
				ArrResult.add(row);
			}

			

		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			rs.close();
			con.close();
			st.close();
		}
		return ArrResult;

	}
	private int  executeSQL(String sql) throws SQLException{
		System.out.println(sql);
		int ID = 0;
		try {
			st = con.createStatement();
			st.execute(sql);
			String queryStr = "select LAST_INSERT_ID()";
			
			ResultSet rs=st.executeQuery(queryStr);
			if ( rs.next() )
			{
			ID = rs.getInt(1);
			}
			System.out.println(ID);
		} catch (SQLException e) {			
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			con.close();
			st.close();
		}
		return ID;
	}

	@Override
	public int  insert(DbRow row) throws SQLException {
		String sql;

		String column = " (";
		String Values = " (";

		column += row.getColumnName(0);
		Values += "'" + row.getColomn(0) + "'";

		for (int i = 1; i < row.getSize(); i++) {
			column += "," + row.getColumnName(i);
			Values += ",'" + row.getColomn(i) + "'";
		}
		column += ")";
		Values += ")";

		sql = "insert into " + this._getName() + column + " values" + Values;

		return this.executeSQL(sql);


	}
	@Override
	public void update(DbRow row, String where) throws SQLException {
		String sql;
		sql="update "+this._getName();
		
		sql += " set "+row.getColumnName(0)+"='"+row.getColomn(0)+"'";
		
		for (int i = 1; i < row.getSize(); i++) {
			sql += ", "+row.getColumnName(i)+"='"+row.getColomn(i)+"'";
		}
		if (where!=null) {
			sql+= " where " + where;
		}

		this.executeSQL(sql);
		
		
	}

	@Override
	public void delete(String where) throws SQLException {
			
		if(! (where==null))
		{
		String sql;
		sql="delete FROM "+ this._getName()+" where "+where;
		
		this.executeSQL(sql);
		}else {
			System.out.println("nu se poate de sters tata tabela");
		}
	}

}
